package com.ruoyi.reportReview.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportReview.domain.bo.IndustrialReportInfoStationBo;
import com.ruoyi.reportReview.domain.vo.IndustrialReportInfoStationVo;
import com.ruoyi.reportReview.domain.IndustrialReportInfoStation;
import com.ruoyi.reportReview.mapper.IndustrialReportInfoStationMapper;
import com.ruoyi.reportReview.service.IIndustrialReportInfoStationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填报对象-场站基本信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-23
 */
@RequiredArgsConstructor
@Service
public class IndustrialReportInfoStationServiceImpl implements IIndustrialReportInfoStationService {

    private final IndustrialReportInfoStationMapper baseMapper;

    /**
     * 查询填报对象-场站基本信息
     */
    @Override
    public IndustrialReportInfoStationVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填报对象-场站基本信息列表
     */
    @Override
    public TableDataInfo<IndustrialReportInfoStationVo> queryPageList(IndustrialReportInfoStationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialReportInfoStation> lqw = buildQueryWrapper(bo);
        Page<IndustrialReportInfoStationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填报对象-场站基本信息列表
     */
    @Override
    public List<IndustrialReportInfoStationVo> queryList(IndustrialReportInfoStationBo bo) {
        LambdaQueryWrapper<IndustrialReportInfoStation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialReportInfoStation> buildQueryWrapper(IndustrialReportInfoStationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialReportInfoStation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStationName()), IndustrialReportInfoStation::getStationName, bo.getStationName());
        lqw.eq(StringUtils.isNotBlank(bo.getStationType()), IndustrialReportInfoStation::getStationType, bo.getStationType());
        lqw.eq(StringUtils.isNotBlank(bo.getMainGoodsType()), IndustrialReportInfoStation::getMainGoodsType, bo.getMainGoodsType());
        lqw.eq(bo.getDataStatus() != null, IndustrialReportInfoStation::getDataStatus, bo.getDataStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUnifiedSocialCode()), IndustrialReportInfoStation::getUnifiedSocialCode, bo.getUnifiedSocialCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorCompany()), IndustrialReportInfoStation::getOperatorCompany, bo.getOperatorCompany());
        lqw.eq(bo.getArea() != null, IndustrialReportInfoStation::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), IndustrialReportInfoStation::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IndustrialReportInfoStation::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IndustrialReportInfoStation::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getBelongParkId()), IndustrialReportInfoStation::getBelongParkId, bo.getBelongParkId());
        lqw.like(StringUtils.isNotBlank(bo.getBelongParkName()), IndustrialReportInfoStation::getBelongParkName, bo.getBelongParkName());
        lqw.eq(StringUtils.isNotBlank(bo.getManager()), IndustrialReportInfoStation::getManager, bo.getManager());
        lqw.eq(StringUtils.isNotBlank(bo.getManagerContact()), IndustrialReportInfoStation::getManagerContact, bo.getManagerContact());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList1()), IndustrialReportInfoStation::getFileList1, bo.getFileList1());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList2()), IndustrialReportInfoStation::getFileList2, bo.getFileList2());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList3()), IndustrialReportInfoStation::getFileList3, bo.getFileList3());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList4()), IndustrialReportInfoStation::getFileList4, bo.getFileList4());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList5()), IndustrialReportInfoStation::getFileList5, bo.getFileList5());
        lqw.eq(bo.getFreightVolume() != null, IndustrialReportInfoStation::getFreightVolume, bo.getFreightVolume());
        lqw.eq(bo.getCarNum() != null, IndustrialReportInfoStation::getCarNum, bo.getCarNum());
        return lqw;
    }

    /**
     * 新增填报对象-场站基本信息
     */
    @Override
    public Boolean insertByBo(IndustrialReportInfoStationBo bo) {
        IndustrialReportInfoStation add = BeanUtil.toBean(bo, IndustrialReportInfoStation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填报对象-场站基本信息
     */
    @Override
    public Boolean updateByBo(IndustrialReportInfoStationBo bo) {
        IndustrialReportInfoStation update = BeanUtil.toBean(bo, IndustrialReportInfoStation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialReportInfoStation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填报对象-场站基本信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
